<?php
/*-------------------------------------------------------+
| PHP-Fusion Content Management System
| Copyright (C) 2002 - 2008 Nick Jones
| http://www.php-fusion.co.uk/
+--------------------------------------------------------+
| Filename: db.php
| Author: Philipp Blanke (PhiBla)
| This page handles the database update if the user
| does NOT have Javascript enabled.
+--------------------------------------------------------+
| This program is released as free software under the
| Affero GPL license. You can redistribute it and/or
| modify it under the terms of this license which you
| can read by viewing the included agpl.txt or online
| at www.gnu.org/licenses/agpl.html. Removal of this
| copyright header is strictly prohibited without
| written permission from the original author(s).
+--------------------------------------------------------*/
/*-------------------------------------------------------+
| Based on
| (Unobtusive) AJAX Rating Bars v 1.2.2
| ryan masuga, masugadesign.com
| ryan@masugadesign.com 
| Licensed under a Creative Commons Attribution 3.0 License.
| http://creativecommons.org/licenses/by/3.0/
--------------------------------------------------------- */

include "../../../../maincore.php";

header("Cache-Control: no-cache");
header("Pragma: nocache");

//getting the values
$vote_sent = preg_replace("/[^0-9]/","",$_GET['j']);
$item_id = (is_numeric($_GET['item_id'])? $_GET['item_id']: '');
$type = (strlen($_GET['type'])== 1 ? $_GET['type'] : '');
$user =  $userdata['user_id'];
$units = 5;
$referer  = $_SERVER['HTTP_REFERER'];
if ($vote_sent > $units) die("Sorry, vote appears to be invalid."); // kill the script because normal users will never see this.

//connecting to the database to get some information
$result=dbquery("SELECT * FROM ".DB_RATINGS." WHERE rating_item_id='".$rating_item_id."' AND rating_type='".$rating_type."' ");
while($row=dbarray($result)){
	$total_value += $row['rating_vote'];
	$votes_total++;
}

$count = $total_votes; //how many votes total
$current_rating = $total_value; //total number of rating added together and stored
$sum = $vote_sent+$current_rating; // add together the current vote value and the total vote value

// checking to see if the first vote has been tallied
// or increment the current number of votes
($sum==0 ? $added=0 : $added=$count+1);

// if it is an array i.e. already has entries the push in another value
//((is_array($checkIP)) ? array_push($checkIP,$ip_num) : $checkIP=array($ip_num));
//$insertip=serialize($checkIP);

//Check if user has voted  
$voted =dbcount(("rating_id"), DB_RATINGS, "rating_user ='".$userdata['user_id']."' AND rating_item_id='".$rating_item_id."' AND rating_type='".$rating_type."'");
	
if(!$voted) {     //if the user hasn't yet voted, then vote normally...

//if (($vote_sent >= 1 && $vote_sent <= $units) && ($ip == $ip_num)) { // keep votes within range
if (($vote_sent >= 1 && $vote_sent <= $units)) { // keep votes within range
	dbquery("INSERT INTO ".DB_RATINGS."(rating_item_id, rating_type, rating_user, rating_vote, rating_datestamp, rating_ip) VALUES ('".$item_id."', '".$type."', '".$user."', '".$vote_sent."', '".time()."', '".USER_IP."')");
} 
} //end for the "if(!$voted)"
redirect($referer); // go back to the page we came from 
exit;
?>